<?php

!defined('IN_HDWIKI') && exit('Access Denied');

class usermodel {

	var $db;
	var $base;

	function usermodel(&$base) {
		$this->base = $base;
		$this->db = $base->db;
	}

	function get_user($field,$value){
		return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."user WHERE  $field='$value'");
	}
	
	function add_referer(){
		if($_SERVER['HTTP_REFERER']){
			$this->db->query("UPDATE ".DB_TABLEPRE."session SET referer ='".$_SERVER['HTTP_REFERER']."' WHERE sid='".base::hgetcookie('sid')."'");
		}
	}
	function get_referer(){
		$session=$this->db->fetch_first("SELECT referer FROM ".DB_TABLEPRE."session  WHERE sid='".base::hgetcookie('sid')."'");
		if($session['referer']==""){
			$session['referer']="index.php";
		}else{
			if(strpos($session['referer'],'admin_')!==false){
				$session['referer']="index.php?admin_main";
			}
		}
		return $session['referer'];
	}
	function  refresh_user($uid){
		$sid=base::hgetcookie('sid');
		$this->base->user=$this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."user u,".DB_TABLEPRE."usergroup g WHERE u.uid=$uid AND u.groupid=g.groupid");
		$session=$this->db->fetch_first("SELECT referer FROM ".DB_TABLEPRE."session  WHERE sid='".$sid."'");
		$this->db->query("REPLACE INTO ".DB_TABLEPRE."session (sid,uid,username,islogin,`time`,referer) VALUES ('$sid',$uid,'".$this->base->user['username']."',1,{$this->base->time},'".$session['referer']."')");
		$this->base->hsetcookie('uid',$uid,24*3600*365);
	}
	
	function update_user($uid,$time,$ip){
		 $this->db->query("UPDATE ".DB_TABLEPRE."user SET lasttime='$time',lastip='$ip' WHERE uid='$uid'");
	}
	
	function user_logout($id,$type=0){
		if($type!==0){
			$this->db->query("DELETE FROM ".DB_TABLEPRE."session WHERE uid='$id'");
		}else{
			$this->base->hsetcookie('sid','',0);
			$this->db->query("DELETE FROM ".DB_TABLEPRE."session WHERE sid='$id'");
		}
	}
	
	function  send_email($to,$subject,$message,$from){
		if('UTF-8'==WIKI_CHARSET){
			$subject=string::hiconv($subject,'gbk','utf-8');
			$message=string::hiconv($message,'gbk','utf-8');
		}
		$mail_config=unserialize($this->base->setting['mail_config']);
		if(isset($mail_config) && !empty($mail_config['mail_host'])){
			require('lib/smtp.class.php');
			$smtp = new smtp ($mail_config['mail_host'],$mail_config['mail_port'],true,$mail_config['mail_user'],$mail_config['mail_pass']);
			$smtp->debug = false;
			$smtp->sendmail($to, $mail_config['mail_mail'], $subject, $message, 'HTML');
		}else{
			require('lib/mail.class.php');
			$m= new Mail;
			$m->From($from);
			$m->To($to);
			$m->Subject($subject);
			$m->Body($message,'gbk');
			$m->Priority(2);
			$m->Send();
		}
	}
	
	function get_admin_email(){
		return $this->db->result_first("SELECT email FROM ".DB_TABLEPRE."user WHERE groupid=4");
	}
	
	function  update_getpass($uid,$code=''){
		if(empty($code)){
			$this->db->query("UPDATE ".DB_TABLEPRE."activation SET available=0 WHERE uid=$uid");
		}else{
			$this->db->query("DELETE  FROM ".DB_TABLEPRE."activation WHERE uid=$uid AND available=0  ");
			$this->db->query("REPLACE INTO  ".DB_TABLEPRE."activation (uid,code,time,type) VALUES ($uid,'$code',{$this->base->time},1)");
		}
	}
	function  get_idstring_by_uid($uid,$time){
		return $this->db->result_first("SELECT code FROM ".DB_TABLEPRE."activation WHERE uid=$uid AND available=1  AND type=1 AND time>($time-3*24*3600) ORDER BY time DESC");
	}
	
	function add_user($username,$password,$email,$time,$ip='127.0.0.1',$groupid=2,$uid='') {
		$language=$this->base->setting['lang_name'];
		$style=$this->base->setting['style_name'];
		
		$checkup = $this->base->setting['register_check'] == '1'? '0' : '1';
		
		if ($this->base->user['islogin'] == 2){	$checkup = 1;}
		
		$credits = 0;
		$usergroup = $this->db->fetch_first('SELECT creditslower,`type` FROM '.DB_TABLEPRE.'usergroup  WHERE  groupid='.$groupid);
		if($usergroup && $usergroup['type'] == 2){
			$credits = $usergroup['creditslower'];
		}
		
		if($uid){
			$sql = "REPLACE INTO  ".DB_TABLEPRE."user (uid,username,`password`,email,regip,regtime,groupid,credits,language,style,checkup) VALUES"
			." ($uid,'$username','$password','$email','$ip',$time,$groupid,$credits,'$language','$style',$checkup)";
		}else{
			$sql ="INSERT INTO  ".DB_TABLEPRE."user (username,`password`,email,regip,regtime,groupid,credits,language,style,checkup) VALUES "
			."('$username','$password','$email','$ip',$time,$groupid,$credits,'$language','$style',$checkup)";
		}
		
		if($this->db->query($sql)){
			$uid = $this->db->insert_id();
			if($credits != 0){
				$this->db->query("INSERT INTO ".DB_TABLEPRE."creditdetail(uid,operation,credit,time) VALUES ($uid,'user-changegroup',$credits,{$this->base->time}) ");
			}
			return $uid;
		}else{
			return false;
		}
	}
	
	function save_code($code,$sid) {
		return $this->db->query("UPDATE ".DB_TABLEPRE."session SET code='$code' WHERE sid='$sid'");
	}
	
	function edit_user($uid, $password, $email, $groupid) {
		$credits = 0;
		$newcredits = 0;
		$diffcredits = 0;
		$sqladd =' ';
		
		$user = $this->get_user('uid',$uid);
		if (is_array($user)){
			$credits = $user['credits'];
		}
		
		$usergroup = $this->db->fetch_first("SELECT `type`,`creditslower` FROM ".DB_TABLEPRE."usergroup WHERE `groupid`=$groupid");
		
		if ($user["groupid"] != $groupid && is_array($usergroup) && $usergroup['type'] == 2){
			$newcredits = $usergroup['creditslower'];
			$diffcredits = $newcredits - $credits;
			$sqladd=" ,credits=$newcredits ";
			$this->db->query("INSERT INTO ".DB_TABLEPRE."creditdetail(uid,operation,credit,time) VALUES ($uid,'user-changegroup',$diffcredits,{$this->base->time}) ");
		}
		if(empty($password)){
			$this->db->query("UPDATE ".DB_TABLEPRE."user SET email='$email' ,groupid=$groupid $sqladd WHERE uid=$uid ");
		}else{
			$password = md5($password);
			$this->db->query("UPDATE ".DB_TABLEPRE."user SET password='$password' ,email='$email' ,groupid=$groupid $sqladd WHERE uid=$uid ");
		}
	}

	function remove_user($uids) {
		if (is_array($uids)){
			$uids = array_diff($uids, array(1));
			if (empty($uids)) {return false;}
			$uidsql = implode($uids,',');
			
			$this->db->query("DELETE FROM  ".DB_TABLEPRE."user WHERE checkup=0 and uid IN ($uidsql) ");
			$this->db->query("UPDATE ".DB_TABLEPRE."user SET groupid=16 WHERE checkup=1 and  uid IN ($uidsql) ");
		}elseif(is_int($uids) && $uid != 1){
			$this->db->query("UPDATE ".DB_TABLEPRE."user SET groupid=16 WHERE checkup=1 and uid = $uids ");
			$this->db->query("DELETE FROM  ".DB_TABLEPRE."user WHERE checkup=0 and uid =$uids ");
		}
	}
	
	function checkup_user($uids, $value=1){
		$uidsql = implode($uids,',');
		$this->db->query("UPDATE ".DB_TABLEPRE."user set checkup=$value WHERE uid IN ($uidsql) ");
	}
	
	function add_credit($uid,$operation,$credit){
		$this->db->query("INSERT INTO ".DB_TABLEPRE."creditdetail(uid,operation,credit,time) VALUES ($uid,'$operation',$credit,{$this->base->time}) ");
		$this->db->query("UPDATE ".DB_TABLEPRE."user SET credits=credits+$credit WHERE uid=$uid ");
		$usergroup = $this->db->fetch_first("SELECT g.`type` FROM ".DB_TABLEPRE."user u,".DB_TABLEPRE."usergroup g WHERE u.uid=$uid AND u.`groupid`=g.`groupid`");
		
		if ($usergroup['type'] == 2){
			$sql = "SELECT g.groupid FROM ".DB_TABLEPRE."user u,".DB_TABLEPRE."usergroup g WHERE u.uid=$uid AND g.`type`=2  AND u.credits >= g.creditslower ORDER BY g.creditslower DESC LIMIT 0,1";
			$usergroup = $this->db->fetch_first($sql);
			
			if (is_array($usergroup) && isset($usergroup['groupid'])){
				$groupid = $usergroup['groupid'];
				$this->db->query("UPDATE ".DB_TABLEPRE."user SET groupid=$groupid WHERE uid=$uid ");
			}
		}
	}
	
	function get_credit($uid){
		$credit = array();
		$query = $this->db->query("SELECT operation, sum(`credit`) AS `total` FROM ".DB_TABLEPRE."creditdetail WHERE uid=$uid GROUP BY operation");
		
		$credit['dailyOperate'] = 0;
		$credit['createDoc'] = 0;
		$credit['editDoc'] = 0;
		$credit['removedDoc'] = 0;
		
		while ($creditdetail = $this->db->fetch_array($query)){
			switch ($creditdetail['operation']){
				case 'doc-create':
					$credit['createDoc'] += $creditdetail['total'];
					break;
				case 'doc-edit':
					$credit['editDoc'] += $creditdetail['total'];
					break;
				case 'remove_doc':
					$credit['removedDoc'] += $creditdetail['total'];
					break;
				default:
					$credit['dailyOperate'] += $creditdetail['total'];
			}
		}
		$credit['creditTotal'] = $credit['dailyOperate'] + $credit['createDoc'] + $credit['editDoc']+ $credit['removedDoc'];
		return $credit;
	}
	
	function get_total_num($username='',$groupid=2, $checkup=1){
		$groupid = (0 == $checkup)? 2: $groupid;
		
		if($groupid==0){
			$dsql='SELECT COUNT(*) FROM '.DB_TABLEPRE.'user where checkup='.$checkup;
		}else{
			$dsql='SELECT COUNT(*) FROM '.DB_TABLEPRE.'user u, '.DB_TABLEPRE.'usergroup g  WHERE u.groupid=g.groupid AND g.groupid='.$groupid.' AND u.checkup='.$checkup;
			if(!empty($username)){
				$dsql.=" AND u.username LIKE '%{$username}%' ";
			}
		}
		return $this->db->result_first($dsql);
	}
	
	function get_list($username='',$groupid=2,$start=0,$limit=10, $checkup=1){
		$userlist=array();
		$sql='SELECT * FROM '.DB_TABLEPRE.'user u, '.DB_TABLEPRE.'usergroup g  WHERE u.groupid=g.groupid';
		
		if($groupid != 0){
		  $sql.=' AND g.groupid='.$groupid;
		}
		
		if(!empty($username)){
		  $sql.=" AND u.username LIKE '%{$username}%' ";
		}
		
		$sql.=" AND u.checkup=$checkup ORDER BY u.uid DESC LIMIT $start,$limit";
		$query=$this->db->query($sql);
		while($user=$this->db->fetch_array($query)){
			$user['lasttime']=$this->base->date($user['lasttime']);
			$user['regtime']=$this->base->date($user['regtime']);
			$userlist[]=$user;
		}
		return $userlist;
	}
	
	function set_profile($gender,$birthday,$location,$signature,$uid){
		$this->db->query("UPDATE `".DB_TABLEPRE."user` SET gender = '$gender',birthday = '$birthday',location = '$location',signature = '$signature' WHERE uid = $uid");		
	}
	
	function update_field($field,$value,$uid,$type=1){
		if($type){
			$sql="UPDATE ".DB_TABLEPRE."user SET $field='$value' WHERE uid= $uid ";
		}else{
			$sql="UPDATE ".DB_TABLEPRE."user SET $field=$field+$value WHERE uid= $uid ";
		}
		$this->db->query($sql);
	} 

	
	function space($uid,$type = 0,$start,$limit){
		$sql=array(
			"SELECT d.did, d.title, d.author, d.authorid, d.time, d.tag, d.summary, d.edits, d.views FROM  ".DB_TABLEPRE."doc d  WHERE authorid = $uid  ORDER BY did DESC LIMIT $start,$limit",
			"SELECT d.did, d.title, d.author, d.authorid, d.time, d.tag, d.summary, d.edits, d.views FROM  ".DB_TABLEPRE."edition  e, ".DB_TABLEPRE."doc d  WHERE e.authorid = $uid AND d.authorid !=$uid AND e.did = d.did GROUP BY d.did ORDER BY eid DESC LIMIT  $start,$limit"
		);
		$doclist = array();
		$query = $this->db->query($sql[$type]);
		while($doc = $this->db->fetch_array($query)){
			$doc['tag'] = explode(";",$doc['tag']);
			$doc['time'] = $this->base->date($doc['time']);
			$doclist[] = $doc;
		}
		return $doclist;
	}
	
 
	
	function update_session($session,$sid){
		$sql="UPDATE ".DB_TABLEPRE."session ";
		foreach($session as $key => $value){
			$sql.=" SET $key ='$value',";
		}
		$sql=substr($sql,0,-1);
		$sql.=" WHERE sid='$sid' ";
		$this->db->query($sql);
	}
	
	function get_popularity_user($start=0,$limit=10){
		$userlist=array();
		$query=$this->db->query("SELECT * FROM ".DB_TABLEPRE."user  WHERE uid>1 ORDER BY views DESC LIMIT $start,$limit");		
		while($user=$this->db->fetch_array($query)){
			$userlist[]=$user;
		}
		return $userlist;
	}
	
	function get_week_num(){
		$week = (int)date('w');
		if(0==$week){$week = 7;}
		
		$endtime = time() - $week * 24 * 60 * 60;
		$starttime = $endtime - 7 * 24 * 60 * 60;
		
		$sql="SELECT COUNT(DISTINCT u.uid) FROM ".DB_TABLEPRE."user u,".DB_TABLEPRE."creditdetail c WHERE u.uid=c.uid AND c.time>$starttime AND c.time <$endtime AND u.groupid!=1  GROUP BY u.uid ";
		return $this->db->result_first($sql);
	}
	
	function get_week_user($start=0,$limit=10,$starttime=0){
		$weekuserlist=array();
		$week=(int)date('w');
		if (0==$week){$week = 7;}
		
		$endtime = time() - $week * 24 * 60 * 60;
		$starttime = $endtime - 7 * 24 * 60 * 60;
		
		$query=$this->db->query("SELECT u.uid, u.username, u.image, sum( c.credit ) credit, u.creates, u.views  FROM ".DB_TABLEPRE."user u,".DB_TABLEPRE."creditdetail c   WHERE u.uid=c.uid AND c.time>$starttime AND c.time <$endtime AND u.groupid!=1  GROUP BY u.uid ORDER BY credit  DESC LIMIT $start,$limit");
		while($user=$this->db->fetch_array($query)){
			$weekuserlist[]=$user;
		}
		return $weekuserlist;
	}
	
	function get_top_user($start=0,$limit=10){
		$userlist=array();
		$query=$this->db->query("SELECT u.uid, u.username,u.credits credit FROM ".DB_TABLEPRE."user u  ORDER BY u.credits  DESC LIMIT $start,$limit");
		while($user=$this->db->fetch_array($query)){
			$userlist[]=$user;
		}
		return $userlist;
	}
	
	function check_name($name){
		$error_names = $this->base->setting['error_names'];
		if (empty($error_names)) return true;
		
		$error_names = explode("\n", $error_names);
		
		foreach($error_names as $value){
			$v = str_replace('*','',$value);
			$v = trim($v);
			if ($v == '') {continue;}
			if ($v == $name) {return false;	}
			
			$pos = strpos($name, $v);
			
			if (substr($value,0,1) == '*' && substr($value,-1) == '*'){
				if (false !== $pos){return false;}
				
			}else if (substr($value,0,1) == '*'){
				if (substr($name, 0-strlen($v)) == $v){return false;}
				
			}else if (substr($value,-1) == '*'){
				if (substr($name, 0, strlen($v)) == $v){return false;}
				
			}else{
				if ($value == $name){return false;}
			}
		}
		return true;
	}
	
	function check_ip($ip){
		$minute = $this->base->setting['register_least_minute'];
		if (empty($minute)) {return true;}
		$user = $this->db->fetch_first("SELECT regtime FROM ".DB_TABLEPRE."user WHERE  `regip`='$ip' order by `uid` desc");
		if ($user && ( 60*$minute+$user['regtime']>time() ) ){return false;}
		return true;
	}
	
	function pp_interface($action,$user,$forward='',$type='phpcms'){
		if(!$forward)$forward = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER']:$this->base->setting['site_url'];
		$jumpurl = PP_API;
		if(PP_NAME=='PHPCMS'){
			$jumpurl.='/member/api/passport_client.php';
		}else if(PP_NAME=='DEDECMS'){
			$jumpurl.='/member/passport_client.php';
			if($action == 'logout'){
				$action = 'quit';
			}
		}else if(PP_NAME=='PHPWIND'){
			$jumpurl.='/passport_client.php';
			if($action == 'logout'){
				$action = 'quit';
			}
		}
		$member=array();
		$member['username']=$user['username'];
		$member['password']=$user['password'];
		$member['email']=$user['email'];
		$member['cktime']=$this->base->time+(60*60*24*365);
		$userdb_encode = '';
		foreach($member as $key=>$val){
			$userdb_encode .= $userdb_encode ? "&$key=$val" : "$key=$val";
		}
		$userdb_encode .= '&time='.$this->base->time;
		$auth = $this->strcode($userdb_encode, 'ENCODE');

		$verify = md5($action.$auth.$forward.PP_KEY);
		$url = $jumpurl.'?action='.$action.'&userdb='.urlencode($auth).'&forward='.urlencode($forward).'&verify='.$verify;

		header('location:'.$url);
		exit;
	}
	
	function strcode($string,$action='ENCODE'){
		$key	= substr(md5($_SERVER["HTTP_USER_AGENT"].PP_KEY),8,18);
		$string	= $action == 'ENCODE' ? $string : base64_decode($string);
		$len	= strlen($key);
		$code	= '';
		for($i=0; $i < strlen($string); $i++){
			$k		= $i % $len;
			$code  .= $string[$i] ^ $key[$k];
		}
		$code = $action == 'DECODE' ? $code : base64_encode($code);
		return $code;
	}
	
}


?>